<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
      class="queryForm"
    >
      <el-form-item label="预约状态:" prop="reservationStatus">
        <el-select
          v-model="queryParams.reservationStatus"
          placeholder="请选择预约状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.reservation_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号:" prop="carNumber">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号:" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约送货日期:">
        <!-- <el-date-picker
          clearable
          v-model="queryParams.sendDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约送货日期"
        >
        </el-date-picker> -->
        <el-date-picker
          v-model="redateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <div class="btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </div>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:reservation:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:reservation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:reservation:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:reservation:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="reservationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column
        label="预约送货日期"
        align="center"
        prop="sendDate"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="预约时间段"
        align="center"
        prop="startSlot,endSlot"
        width="100"
      >
        <template slot-scope="scope">
          {{ scope.row.startSlot }}~{{ scope.row.endSlot }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="预约结束时间" align="center" prop="endSlot" /> -->
      <el-table-column label="车牌号" align="center" prop="carNumber" />
      <el-table-column label="送货渠道" align="center" prop="sendChannel">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.send_channel"
            :value="scope.row.sendChannel"
          />
        </template>
      </el-table-column>
      <el-table-column label="物流类型" align="center" prop="logisticsType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.logistics_type"
            :value="scope.row.logisticsType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="送货订单数"
        align="center"
        prop="sendOrder"
        width="60"
      />
      <el-table-column label="送货件数" align="center" prop="sendCount" />
      <el-table-column label="货物类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.goods_type"
            :value="scope.row.goodsType"
          />
        </template>
      </el-table-column>
      <el-table-column label="仓库类型" align="center" prop="warehouse">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.warehouse"
            :value="scope.row.warehouse"
          />
        </template>
      </el-table-column>
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="司机手机号" align="center" prop="phone" />
      <el-table-column
        label="过号日期"
        align="center"
        prop="outDate"
        width="100"
      />
      <el-table-column
        label="过号次数"
        align="center"
        prop="outNum"
        width="60"
      />
      <!-- <el-table-column label="审单人" align="center" prop="checkPerson" />
      <el-table-column
        label="审单日期"
        align="center"
        prop="checkDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货月台" align="center" prop="reservationNum" />
      <el-table-column
        label="收货完成日期"
        align="center"
        prop="takeDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.takeDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="窗口名称" align="center" prop="windowName" />
      <el-table-column label="预约状态" align="center" prop="reservationStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reservation_status"
            :value="scope.row.reservationStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否大件" align="center" prop="isBig" />
      <el-table-column
        label="审单开始时间"
        align="center"
        prop="inDate"
        width="100"
      />
      <el-table-column
        label="审单完成时间"
        align="center"
        prop="checkDate"
        width="100"
      />
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:reservation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:reservation:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-position"
            :disabled="scope.row.reservationStatus != '0'"
            @click="handleSignin(scope.row)"
            >签到</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="720px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="司机姓名" prop="driverName">
              <el-input
                v-model="form.driverName"
                placeholder="请输入司机姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入司机手机号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预约送货日期" prop="sendDate">
              <el-date-picker
                clearable
                v-model="form.sendDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择预约送货日期"
                @change="handleDateChange"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="carNumber">
              <el-input v-model="form.carNumber" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="送货订单数" prop="sendOrder">
              <el-input
                v-model="form.sendOrder"
                placeholder="请输入送货订单数"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送货件数" prop="sendCount">
              <el-input v-model="form.sendCount" placeholder="请输入送货件数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="时间段" prop="timeId">
              <el-select v-model="form.timeId" placeholder="请选择时间段">
                <el-option
                  v-for="dict in timedataList"
                  :key="dict.id"
                  :label="dict.timeslot"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货物类型" prop="goodsType">
              <el-select v-model="form.goodsType" placeholder="请选择货物类型">
                <el-option
                  v-for="dict in dict.type.goods_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="审单人" prop="checkPerson">
              <el-input v-model="form.checkPerson" placeholder="请输入审单人" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="送货渠道" prop="sendChannel">
              <el-select
                v-model="form.sendChannel"
                placeholder="请选择送货渠道"
              >
                <el-option
                  v-for="dict in dict.type.send_channel"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.sendChannel == '1'">
            <el-form-item label="物流类型" prop="logisticsType">
              <el-select
                v-model="form.logisticsType"
                placeholder="请选择物流类型"
              >
                <el-option
                  v-for="dict in dict.type.logistics_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="审单日期" prop="checkDate">
              <el-date-picker
                clearable
                v-model="form.checkDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择审单日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货月台" prop="reservationNum">
              <el-input
                v-model="form.reservationNum"
                placeholder="请输入收货月台"
              />
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="收货完成日期" prop="takeDate">
              <el-date-picker
                clearable
                v-model="form.takeDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择收货完成日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约状态" prop="reservationStatus">
              <el-select
                v-model="form.reservationStatus"
                placeholder="请选择预约状态"
              >
                <el-option
                  v-for="dict in dict.type.reservation_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-row>
          <el-col class="info"> 仓库信息： </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库:" prop="warehouse">
              <el-select v-model="form.warehouse" placeholder="请选择仓库类型">
                <el-option
                  v-for="dict in dict.type.warehouse"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="number">
            当日还可以预约<span class="numberval">{{ overNumber }}</span
            >件
          </el-col>
        </el-row>
        <el-row>
          <el-col class="address">
            仓库地址：陕西省西安市未央区数字医药产业园
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReservation,
  getReservation,
  delReservation,
  addReservation,
  updateReservation,
  listTimes,
} from "@/api/system/reservation";
import { getselectOverCount } from "@/api/system/reservation";

export default {
  name: "Reservation",
  dicts: [
    "reservation_status",
    "time_slot",
    "send_channel",
    "goods_type",
    "warehouse",
    "logistics_type",
  ],
  data() {
    return {
      // 送货预约时间范围限制
      // pickersendDate: {
      //   disabledDate(time) {
      //     const today = new Date();
      //     const yesterday = new Date(today);
      //     console.log(234, yesterday.setDate(today.getDate() - 1));
      //     yesterday.setDate(today.getDate() - 1);
      //     return time.getTime() < yesterday.getTime();
      //   },
      // },
      // 预约日期范围
      // dateRange: ["",""],
      // 预约日期范围
      redateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约管理表格数据
      reservationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sendDate: null,
        carNumber: null,
        checkPerson: null,
        checkDate: null,
        reservationNum: null,
        takeDate: null,
        reservationStatus: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sendDate: [
          { required: true, message: "预约送货日期不能为空", trigger: "blur" },
        ],
        // startSlot: [
        //   {
        //     required: true,
        //     message: "预约开始时间段不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // endSlot: [
        //   {
        //     required: true,
        //     message: "预约开始时间段不能为空",
        //     trigger: "blur",
        //   },
        // ],
        carNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" },
        ],
        sendChannel: [
          { required: true, message: "送货渠道不能为空", trigger: "blur" },
        ],
        sendCount: [
          { required: true, message: "送货件数不能为空", trigger: "blur" },
        ],
        goodsType: [
          { required: true, message: "货物类型不能为空", trigger: "blur" },
        ],
        driverName: [
          { required: true, message: "司机姓名不能为空", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "司机手机号不能为空", trigger: "blur" },
        ],
        reservationStatus: [
          { required: true, message: "预约状态不能为空", trigger: "change" },
        ],
        sendOrder: [
          { required: true, message: "送货订单数不能为空", trigger: "blur" },
        ],
        timeId: [
          { required: true, message: "预约时间段不能为空", trigger: "blur" },
        ],
        warehouse: [
          { required: true, message: "仓库类型不能为空", trigger: "blur" },
        ],
      },
      // 剩余预约数量
      overNumber: null,
      // pc时间段下拉框
      timedataList: [],
    };
  },
  watch: {
    "$route.query.reservationStatus": function (newVal, oldVal) {
      if (newVal !== oldVal) {
        this.getValueList();
      }
    },
  },
  created() {
    if (this.$route.query.reservationStatus) {
      this.getValueList();
    } else {
      this.getList();
    }
  },
  mounted() {
    this.pctimesList();
  },

  methods: {
    pctimesList() {
      listTimes().then((res) => {
        console.log(res);
        this.timedataList = res.data.map((item) => {
          item.timeslot = item.startSlot + "~" + item.endSlot;
          return item;
        });
      });
    },
    // 签到
    handleSignin(row) {
      this.$modal
        .confirm("是否确认签到?")
        .then(() => {
          updateReservation({ id: row.id, reservationStatus: "1" }).then(
            (res) => {
              this.getList();
              this.$modal.msgSuccess("签到成功");
            }
          );
        })
        .catch(() => {});
    },
    // 触发预约送货日期 剩余预约数量
    handleDateChange(value) {
      console.log(value);
      getselectOverCount({ setDate: value }).then((res) => {
        this.overNumber = res.data;
        console.log(res);
      });
    },
    /** 查询预约管理列表 */
    // getList() {
    //   this.loading = true;
    //   listReservation(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
    //     this.reservationList = response.rows;
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    getList() {
      this.loading = true;
      // this.$set(this.queryParams, "startSlot", this.dateRange[0]);
      // this.$set(this.queryParams, "endSlot", this.dateRange[1]);
      listReservation(
        this.addDateRange(this.queryParams, this.redateRange)
      ).then((response) => {
        this.reservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 传参更新列表
    getValueList() {
      this.loading = true;
      listReservation({
        reservationStatus: this.$route.query.reservationStatus,
        sendDate: this.$route.query.sendDate,
      }).then((response) => {
        this.queryParams.reservationStatus =
          this.$route.query.reservationStatus;
        this.queryParams.sendDate = this.$route.query.sendDate;
        this.reservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sendDate: null,
        timeId: null,
        carNumber: null,
        sendChannel: null,
        sendCount: null,
        goodsType: null,
        driverName: null,
        phone: null,
        checkPerson: null,
        checkDate: null,
        reservationNum: null,
        takeDate: null,
        reservationStatus: null,
        warehouse: null,
        sendOrder: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      (this.dateRange = ["", ""]), (this.redateRange = []), this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getReservation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约管理";
        getselectOverCount({ setDate: this.form.sendDate }).then((res) => {
          this.overNumber = res.data;
          console.log(res);
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateReservation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReservation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate((valid) => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         if (this.form.sendCount <= this.overNumber) {
    //           updateReservation(this.form).then((response) => {
    //             this.$modal.msgSuccess("修改成功");
    //             this.open = false;
    //             this.getList();
    //           });
    //         } else {
    //           this.$modal.msgError("送货件数不能大于剩余预约数");
    //         }
    //       } else {
    //         if (this.form.sendCount <= this.overNumber) {
    //           addReservation(this.form).then((response) => {
    //             this.$modal.msgSuccess("新增成功");
    //             this.open = false;
    //             this.getList();
    //           });
    //         } else {
    //           this.$modal.msgError("送货件数不能大于剩余预约数");
    //         }
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除预约管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delReservation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/reservation/export",
        {
          ...this.queryParams,
        },
        `reservation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
.el-select {
  width: 220px;
}
.el-input--small {
  width: 220px;
}
.address {
  vertical-align: middle;
  float: left;
  font-weight: 700;
  font-size: 14px;
  color: #606266;
  padding: 0 12px 0 0;
  box-sizing: border-box;
  margin-left: 30px;
}
.info {
  vertical-align: middle;
  float: left;
  font-weight: 700;
  font-size: 14px;
  color: #606266;
  padding: 20px 12px 20px 0;
  box-sizing: border-box;
  margin-left: 30px;
}
.number {
  padding: 12px 12px 12px 20px;
  font-weight: 700;
  .numberval {
    color: #f8572f;
  }
}

// .queryForm {
//   ::v-deep .el-range-editor.el-input__inner {
//     width: 295px !important;
//   }
//   ::v-deep .el-input {
//     width: 295px !important;
//   }
//   .btn{
//     margin-left: 700px;
//   }
// }
</style>
